import { createRouter, createWebHistory } from 'vue-router'
import Main from 'components/main'
import store from 'store'
import article from './modules/article'
import setting from './modules/setting'
import product from './modules/product'

const routes = [
  {
    path: '/admin',
    name: 'Main',
    meta:{
      title:'后台管理系统'
    },
    component: Main,
    children:[
      {
        path:'',
        name:'home',
        component:()=>import('../views/Home.vue')
      },
      {
        path:'/:catchAll(.*)*',
        name:404,
        component:()=>import('../views/404.vue')
      },
      ...article,
      ...setting,
      ...product,
    ]
  },
  {
    path:'/:catchAll(.*)*',
    name:404,
    component:()=>import('../views/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from)=>{
  // 配置meta
  if(to.meta.title){
    document.title = to.meta.title
  }else{
    document.title = '后台管理系统'
  }
  if(from.path == '/'){
    store.dispatch('menu/start',to.path)
  }else{
    store.dispatch('menu/dealMenu',to.path)
  }
  // console.log(to,from)
})

export default router
